package xyz.playedu.api.controller.backend;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xyz.playedu.api.request.backend.ResourceTextCreateRequest;
import xyz.playedu.api.request.backend.ResourceTextCreateResponse;
import xyz.playedu.api.request.backend.ResourceTextRequest;
import xyz.playedu.common.annotation.BackendPermission;
import xyz.playedu.common.annotation.Log;
import xyz.playedu.common.bus.BackendBus;
import xyz.playedu.common.constant.BPermissionConstant;
import xyz.playedu.common.constant.BusinessTypeConstant;
import xyz.playedu.common.domain.AdminRole;
import xyz.playedu.common.service.AdminUserService;
import xyz.playedu.common.types.JsonResponse;
import xyz.playedu.resource.domain.Resource;
import xyz.playedu.resource.domain.ResourceText;
import xyz.playedu.resource.domain.ResourceTextResponse;
import xyz.playedu.resource.service.ResourceService;
import xyz.playedu.resource.service.ResourceTextService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/backend/v1/resource-text")
@Slf4j
public class ResourceTextController {


    @Autowired
    private AdminUserService adminUserService;


    @Autowired private BackendBus backendBus;


    @Autowired private ResourceTextService resourceTextService;

    @Autowired
    private ResourceService resourceService;

    @GetMapping("/index/{id}")
    @Log(title = "查询文本", businessType = BusinessTypeConstant.GET)
    public JsonResponse index(@PathVariable(name = "id") Integer rid){
        Resource resource  = resourceService.getById(rid);
        Map<String,Object> resultData=new HashMap<>();
        List<ResourceText> resourceTexts = resourceTextService.getResourceTextByRid(rid);
        resultData.put("list",resourceTexts);
        if(resource!=null){
            resultData.put("name",resource.getName());
            resultData.put("duration",resource.getSize());
        }else{
            resultData.put("name","");
            resultData.put("duration",10);
        }
        return JsonResponse.data(resultData);
    }

    @PostMapping("/create")
    @Log(title = "新建文本", businessType = BusinessTypeConstant.INSERT)
    public JsonResponse create(@RequestBody ResourceTextCreateRequest resourceTextCreateRequest) {
        List<ResourceTextRequest> resourceTextRequests = resourceTextCreateRequest.getResourceList();
        List<ResourceText> resourceTexts=new ArrayList<>();
        resourceTextRequests.forEach(resourceTextRequest -> {
            ResourceText resourceText=new ResourceText();
            resourceText.setId(resourceTextRequest.getId());
            resourceText.setRid(resourceTextRequest.getRid());
            resourceText.setTitle(resourceTextRequest.getTitle());
            resourceText.setContent(resourceTextRequest.getContent());
            resourceTexts.add(resourceText);
        });
        ResourceTextResponse  resourceTextResponse= resourceTextService.saveResourceTexts(resourceTextCreateRequest.getCategoryId(),
                resourceTextCreateRequest.getRid(),
                resourceTextCreateRequest.getName(),
                resourceTextCreateRequest.getDuration(),
                resourceTexts);
        return JsonResponse.data(resourceTextResponse);
    }

}
